//
//  JDQuickNewsRecordViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/8/19.
//

import UIKit

class JDQuickNewsRecordViewController: JDBaseViewController {
    
    var mainTableView: UITableView?

    var recordModels: [JDBusinessNewsListModel?]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        self.getNetData()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupLeftBackItem()
        self.setupNavigationTitle("快讯记录")
    }
    
    func setupUIElements() {
        self.mainTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.mainTableView!)
        self.mainTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.view).offset(statusBarHeight+44)
            make.bottom.equalTo(self.view)
        })
        self.mainTableView?.backgroundColor = .commonColor()
        self.mainTableView?.delegate = self
        self.mainTableView?.dataSource = self
        self.mainTableView?.register(JDQuickNewsTableViewCell.self, forCellReuseIdentifier: "quickCell")
        self.mainTableView?.addRefreshHeader { [weak self] in
            self?.currentPage = 1
            self?.getNetData(true)
        }
        
        self.mainTableView?.addRefreshFooter { [weak self] in
            self?.currentPage += 1
            self?.getNetData(false)
        }
    }

    override func getNetData(_ isRefresh: Bool = true) {
//        getMyBusinessNewsRecordList
        
        var params = [String: Any]()
        params["businessTypeCode"] = "0"
        params["pageNumber"] = self.currentPage
        params["pageSize"] = 10

        JDNetWorkRequest(.getMyBusinessNewsRecordList(params)) { responseStr in
            
            printLog("快讯记录:\(responseStr)")
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
//                var dataSource: [JDBusinessNewsListModel?]?
                
                if let models = [JDBusinessNewsListModel].deserialize(from: responseStr, designatedPath: "data.records") {
                    
                    if isRefresh {
                        self.recordModels = models
                    } else {
                        self.recordModels?.append(contentsOf: models)
                    }
                }
//            快讯记录:{"msg":"ok","data":{"records":[{"picUrl2":"string","content":"string","registrationEnabledFlag":false,"id":1,"publishDate":"18天前","picUrl3":"string","videoUrl":"string","publishUserId":1,"name":"string","picUrl1":"string"}],"pageNumber":1,"totalRow":1,"pageSize":10,"totalPage":1},"code":0}
                
            }
            self.mainTableView?.reloadData()
            self.mainTableView?.endRefresh()
            
        } failure: { error in
            self.mainTableView?.reloadData()
            self.mainTableView?.endRefresh()
            
        } error: { errorCode in
            self.mainTableView?.reloadData()
            self.mainTableView?.endRefresh()
            
        }

        
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDQuickNewsRecordViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.recordModels?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "quickCell") as? JDQuickNewsTableViewCell
        
        let currentModel = self.recordModels?[indexPath.row]
        cell?.businessNewsModel = currentModel
        
        cell?.addDragonUnion?.isHidden = true
        cell?.topImgV?.isHidden = true
        cell?.callButton?.isHidden = true
        cell?.newsTypeLabel?.isHidden = true
        
        return cell!
    }
}
